<?php

namespace app\Manage\controller;

use app\common\controller\Manage;
use app\common\model\BillAftersales;
use app\common\model\Navigation;
use app\common\model\Notice;
use app\common\model\Operation;
use app\common\model\Order;
use app\common\model\PintuanGoods;
use app\common\model\Promotion;
use app\common\model\User;
use app\common\model\UserGrade;
use think\Console;
use think\facade\Cache;
use app\common\model\WeixinAuthor;
use app\common\model\Goods;
use app\common\model\Brand;
use app\common\model\Product;

class Index extends Manage
{
	public function index()
	{
		$operationModel = new Operation();
		$this->assign('menu', $operationModel->manageMenu(session('manage')['id']));
		return $this->fetch('index');
	}

	public function welcome()
	{
		//        $orderModel = new Order();
		//        //未发货数量
		//        $unpaid_count = $orderModel->where(['status' => 1, 'pay_status' => 1, 'ship_status' => 1])->count();
		//        //待发货数量
		//        $unship_count = $orderModel->where(['status' => 1, 'pay_status' => 2, 'ship_status' => [1, 2]])->count();
		//        //待售后数量
		//        $billAfterSalesModel = new BillAftersales();
		//        $afterSales_count    = $billAfterSalesModel->getCount();
		//
		//        $this->assign('unpaid_count', $unpaid_count);
		//        $this->assign('unship_count', $unship_count);
		//        $this->assign('after_sales_count', $afterSales_count);
		//
		//        $goodsModel   = new Goods();
		//        $goodsStatics = $goodsModel->staticGoods();
		//        $this->assign('goods_statics', $goodsStatics);
		//        hook('adminindex', $this); //后台首页钩子
		return $this->fetch('welcome');
	}

	public function nav()
	{
		if ($this->request->isAjax()) {
			$param = input('param.');
			return (new Navigation())->tableData($param);
		}
		return $this->fetch('nav');
	}

	public function apply()
	{
		if ($this->request->isAjax()) {
			$param = input('param.');
			$param['type'] = 8;
			return (new Navigation())->tableData($param);
		}
		return $this->fetch('apply');
	}

	public function add()
	{
		if ($this->request->isPost()) {
			return (new Navigation())->addData(input('param.'));
		}
		return $this->fetch('add');
	}

	public function edit()
	{
		if ($this->request->isPost()) {
			$data = [
				'id'     => input('post.id'),
				'title'  => input('post.title'),
				'status' => input('post.status')
			];
			return (new Navigation())->saveData($data);
		}
		$info = (new Navigation())->get(input('param.id/d'));
		if (! $info) {
			return error_code(10002);
		}
		$type = input('param.type/d');
		return $this->fetch('edit', [
			'info' => $info,
			'type' => $type
		]);
	}

	/**
	 * 清除整站全部缓存
	 * 如果其它地方写了缓存的读写方法，一定要有判断是否有缓存的情况！！！
	 */
	public function clearCache()
	{
		Cache::clear(); //TODO 如果开启其他缓存，记得这里要配置缓存配置信息
		Console::call('clear', [
			'--cache',
			'--dir'
		]); //清除缓存文件
		Console::call('clear', [
			'--path',
			ROOT_PATH . '\\runtime\\temp\\'
		]); //清除模板缓存
		//删除海报
		del_dir_and_file(ROOT_PATH . 'public' . DS . 'static' . DS . 'poster');
		$this->success('清除缓存成功', 'index/welcome');
	}
}
